package core2.ch02xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlParser {
	static Document document;

	private boolean validating;

	private String fileName;

	public XmlParser(String fileName) {
		this.fileName = fileName;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		document = builder.newDocument();
	}

	public List readXml(String nodeName) {
		List<String> contents =new ArrayList<String>();
		String content =null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			factory.setValidating(validating);
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse(new File(fileName));
			document.getDocumentElement().normalize();
			Node root = document.getFirstChild();
			NodeList nodes = root.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				Node nodeItem = nodes.item(i);
				if (nodeItem.getNodeName().equals(nodeName)) {
					content = nodeItem.getFirstChild().getNodeValue();
					contents.add(content);
				}
			}
		} catch (Exception exp) {
			exp.printStackTrace();
			return null;
		}
		return contents;
	}

	public void writeXml(String rootName, String nodeName, List contents) {
		Element root = document.createElement(rootName);
		document.appendChild(root);
		Element node=null;
		String content=null;
		for (Iterator iter = contents.iterator(); iter.hasNext();) {
			content = (String) iter.next();
			node = document.createElement(nodeName);
			node.appendChild(document.createTextNode(content));
			root.appendChild(node);
			
		}
		
		try {
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			DOMSource source = new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
		} catch (TransformerException mye) {
			mye.printStackTrace();
		} catch (IOException exp) {
			exp.printStackTrace();
		}
	}
	
	public static void main(String [] args){
		XmlParser xp =new XmlParser("src\\parse\\xml\\XmlParser.xml");
		List<String> contents =new ArrayList<String>();
		
//		contents.add("fdasf2");
//		contents.add("fdasf3");
//		contents.add("fdasf4");
//		xp.writeXml("HAHA", "gg", contents);
//		System.out.print("Write OK");
		
		contents=xp.readXml("gg");
		String temp=null;
		for (Iterator iter = contents.iterator(); iter.hasNext();) {
			temp = (String) iter.next();
			System.out.println(temp);
		}
	}

}
